package com.lyy.lottery.service.activitystatus.operator;

import com.lyy.lottery.dao.dataobject.ActivityDo;
import com.lyy.lottery.dao.mapper.ActivityMapper;
import com.lyy.lottery.dao.mapper.ActivityPrizeMapper;
import com.lyy.lottery.service.dto.ConvertActivityStatusDTO;
import com.lyy.lottery.service.enums.ActivityPrizeStatusEnum;
import com.lyy.lottery.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetStatus=convertActivityStatusDTO.getTargetActivityStatus();
        if (null == activityId || null == targetStatus) return false;

        ActivityDo activityDo=activityMapper.selectById(activityId);
        if (null==activityDo) return false;

        if (targetStatus.name().equalsIgnoreCase(activityDo.getStatus())) return false;

        //判断奖品是否全部抽完：查询 INIT的奖品个数
        int count=activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if (count > 0) return false;

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
